#include "sock_define.h"

extern void ocp_err_quit(const char* error_output);

void cli_login(int sockfd,const char* user_id);


int main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in servaddr;
	if(argc!=3)	
	{
		printf("usage: tcpcli ipaddress user_id\n");
		exit(-1);		
	}
	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(LOGIN_PORT);
	inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	connect(sockfd, (SA *) &servaddr, sizeof(servaddr));
	cli_login(sockfd, argv[2]);

}

void cli_login(int sockfd, const char* user_id)
{
	
	Message_Login buf_send;
	Message_Login buf_recv;		
	buf_send.id =  atoi(user_id);
	
	write(sockfd, (void*) &buf_send,  sizeof(buf_send));
	if(read(sockfd, (void*)&buf_recv,  sizeof(buf_send)) == 0)
		ocp_err_quit("server terminated prematurely");
	
	printf("%s ask server to login id(return) = %d result = %c\n", user_id, buf_recv.id, buf_recv.result);	
	
}


